CmdUtils.CreateCommand({  
//to subscribe to the command, go to http://gist.github.com/15544
//code maintained at http://code.google.com/p/tl-ubiquity-commands/
    author: {name: "thomas lemberger", email: "thomas.lemberger@embo.org"},
    license: "GPL",
    name: "more-like-this",  
    takes: {"selected text": noun_arb_text},  
    help: "The command is based on Jane (http://www.biosemantics.org/jane/, Schuemie & Kors (2008) Bioinformatics.24:727). It lists potential experts and articles related to the selected text (typically an abstract).", 
    execute: function(directObj) {
      var selectedText=directObj.text;
      params={//as used on the form submitted from the webpage
         text: selectedText,
         findauthors: "Find authors"        
      };
      jQuery.ajax(
        {type: "POST",
         data: params,
         url: "http://www.biosemantics.org/jane/suggestions.php",
         dataType: "text",
         success: function(html){
           var  newTab = Application.activeWindow.open(Utils.url("http://www.biosemantics.org/jane/suggestions.php"));
           newTab.events.addListener("load", function() { newTab.document.body.innerHTML = html });
           newTab.focus()
         }
        }
      )
    },
    preview: function(pblock, directObj) {
      var selectedText=directObj.text;
      pblock.innerHTML="Querying Jane...";
      params={
         text: selectedText
      };
      jQuery.ajax(
        {type: "POST",
         data: params,
         url: "http://biosemantics.org:8080/jane/authors",
         dataType: "xml",
         success: function(response){
            var names=[];
            var articles={};
            var pmids=[]; pmids[0]=[];//array of arrays
            jQuery("results>author",response).each(function(i){
               var section=this;
               var name=jQuery("author:first",section).text();//author's name is there
               if (name){
                 names.push(name);
                 pmids[name]=[];
                 jQuery("evidence>article",section).each(function(j){//goes through articles assoc with author
                    var article=jQuery(this);
                    var score=parseInt(article.attr("score"));
                    var title=jQuery("title",article).text();
                    var journal=jQuery("journalname",article).text();
                    var year=jQuery("year",article).text();
                    var pmid=jQuery("pmid",article).text();
                    var authors=[];
                    jQuery("authors>author",article).each(function(){authors.push(jQuery(this).text())});
                    if (pmid){
                      pmids[name].push(pmid);
                      articles[pmid]={au: authors, ti: title, jo: journal, yr: year, score: score};
                    };

                    //pblock.innerHTML="Parsing results:<br>"+title;
                 });
               };
            });       

            //after http://news.hping.org/comp.lang.javascript.archive/0867.html
            function sortAssoc(aInput){
              var aTemp = [];
              for (var sKey in aInput) aTemp.push([sKey, aInput[sKey]]);
              aTemp.sort(function () {return arguments[0][1].score > arguments[1][1].score});
                var aOutput = [];
                for (var nIndex = aTemp.length-1; nIndex >=0; nIndex--)
                aOutput[aTemp[nIndex][0]] = aTemp[nIndex][1];
              return aOutput;
            };
    
            var orderedScores=sortAssoc(articles);
            const MAX_DISPLAYED_TITLES=12;

            var html="";
            var style="<style type='text/css'>"+
                      "span.header {position: relative; bottom:1px;font: small-caps bold 10px arial; padding:2px 2px 0px 2px; background-color:white; color: black} "+
                      "#headerTitles{cursor:pointer} "+
                      "#headerTitles, #details {color:black;border: solid 1px white} "+
                      ".displayTitles, .dyn {font: 10px arial; padding:10px 5px; background-color:white;color: #666666} "+
                      "a {text-decoration: none}"+
                      "a:hover {text-decoration: underline; color: red} "+
                      ".dyn {font: 12px arial} "+
                      "dyn li {padding: 3px 0px} "+
                      "displayTitles li {padding: 2px 0px}"+
                      "span.stars {color:red;font-style:bold}"+
                      "</style>";

            html+="<p>Similar PubMed abstracts (via <b><a style='color:yellow' href='http://www.biosemantics.org/jane/'>Jane</a></b>):</p>";

            html+="<span id='headerTitles' class='header'>Titles</span>";

            html+="<div class='displayTitles' id='titles'>";

            var count=0;
            html+="<ul>";
            for (pmid in orderedScores){
               var article=articles[pmid];
               var s=Math.round(article.score/10);
               var stars="";
               for (var j=1;j<=s;j++){stars+="*"};

               var names=article.au;
               var linkedNames="";
               for (i in names){
                  var name=names[i];
                  linkedNames+="<a href='http://www.ncbi.nlm.nih.gov/pubmed/"+pmids[name].join(",")+"'>"+name.replace(/ /,"&nbsp;")+"</a>, ";
               };
               linkedNames=linkedNames.replace(/, $/,"");//remove last comma at the end
               //var source="<span class='journal'>"+article.jo+" ("+article.yr+")</span>";

               html+="<li><a href='http://www.ncbi.nlm.nih.gov/pubmed/"+pmid+"' onmouseover='document.getElementById(\"rel\").innerHTML=unescape(\""+escape(linkedNames)+"\")'>"+article.ti+"</a>&nbsp;<span class='stars'>"+stars+"</span></li>";
            count++;
            if (count>MAX_DISPLAYED_TITLES) break;

            };
            html+="</ul>";
            html+="</div>";
            html+="<br/><br/>";

            html+="<span id='details' class='header'>Authors:</span>";
            html+="<div id='rel' class='dyn'></div>";

            pblock.innerHTML=style+html;
         }
        }
      )
    }
})